/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2019-06-03 09:49:47
 * @LastEditors: kl2018
 * @LastEditTime: 2020-08-18 10:30:12
 */
import React, { Component } from 'react';
import { WeaUpload, WeaLocaleProvider, WeaDialog } from 'ecCom';
import { Button, message } from 'antd';

const { getLabel } = WeaLocaleProvider;

export default class UploadFileComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      limitType: 'docx,doc,wps',
      loading: true,
    };
  }

  componentDidMount() {
    const uploadDocComponent = document.getElementById('uploadDocComponent');
    let limitType = '';
    if (uploadDocComponent && uploadDocComponent.getAttribute('limitType')) {
      limitType = uploadDocComponent.getAttribute('limitType');
    }
    /*eslint-disable*/
    this.setState({ loading: false, limitType });
    /* eslint-enable */
  }

  render() {
    const { loading, limitType } = this.state;
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@q792ce`}
        title={getLabel('20798', '上传文件')}
        icon="icon-coms-official"
        onCancel={this.removeDialog}
        visible={true}
        style={{ width: 500, height: 300 }}
        hasScroll
        maskClosable={true}
        parentClassName="uploadDocComponent"
      >
        {
          !loading &&
          <div style={{ display: 'inline-block', width: '80px', height: '35px', margin: '130px 210px' }}>
            <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@h30ct8`}
              uploadUrl="/api/doc/upload/uploadFile"
              category="string"
              maxFilesNumber={1}
              maxUploadSize={100}
              listType="list"
              limitType={limitType}
              clearWhenReset={false}
              showClearAll={true}
              onChange={(ids, list) => this.onChange(ids, list)}
            >
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fqh73m`} type="primary" >{getLabel(125333, '选择文件')}</Button>
            </WeaUpload>
          </div>
        }
      </WeaDialog>
    );
  }

  onChange(ids = [], list) {
    const limitType = list[0] && list[0].fileExtendName;
    if (ids.length == 0) {
      message.error('25389', '上传失败');
      return;
    }
    this.removeDialog();
    window.uploadLoacalFile(ids[0], limitType);
  }

  removeDialog = () => {
    try {
      let uploadPDFParent = document.getElementsByClassName('uploadDocComponent')[0];
      if (uploadPDFParent == null) {
        uploadPDFParent = parent.document.getElementsByClassName('uploadDocComponent')[0];
      }
      const uploadDocComponent = document.getElementById('uploadDocComponent');
      uploadDocComponent.parentNode.removeChild(uploadDocComponent);
      uploadPDFParent.parentNode.removeChild(uploadPDFParent);
    } catch (e) {
      window.console && console.log(e);
    }
  }
}
